import cv2 as cv

import myutils


def showImage(name, image):
    a = 1
    cv.imshow(name, image)
    cv.waitKey(0)
    cv.destroyAllWindows()
def refer(image):
    ref = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    showImage("ref", ref)
    ref = cv.threshold(ref, 10, 255, cv.THRESH_BINARY_INV)[1]
    showImage("ref", ref)
    refCnts, hierarchy = cv.findContours(ref, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    cv.drawContours(image, refCnts, -1, (0, 0, 255), 3)
    showImage("img", image)
    refCnts = myutils.sort_contours(refCnts, method="leleft-to-right")[0]
    digits = {}
    for (i, c) in enumerate(refCnts):
        (x, y, w, h) = cv.boundingRect(c)
        roi = ref[y:y+h, x:x+w]
        roi = cv.resize(roi, (57, 88))
        showImage("rio", roi)
        digits[i] = roi
    return digits
img = cv.imread("image/ocr_a_reference.png")
refer(img)
